home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 23 / CU Amiga - Super CD-ROM 23 (June 1998).iso / CreatingGames / Utilities / Misc / GMS / GMSDev / Source / C / 3DObjects / Enneper.c next >
Encoding:
C/C++ Source or Header  |  1997-12-16  |  4.3 KB  |  167 lines

  1. /* SAS/C: sc Enneper.c opt math=standard INCDIR=INCLUDES:
  2. **
  3. ** This is a demo of a rotating Enneper's Surface.  The object is
  4. ** pre-calculated then rotated in real time for speed, although things could
  5. ** be faster than this.
  6. **
  7. ** Improve: Use a pixel list rather than calling DrawPixel().
  8. **          Replace doubles with longs/words if possible.
  9. */
  10.  
  11. #include <proto/dpkernel.h>
  12. #include <math.h>
  13.  
  14. BYTE *ProgName      = "Enneper's Surface";
  15. BYTE *ProgAuthor    = "Paul Manias";
  16. BYTE *ProgDate      = "December 1997";
  17. BYTE *ProgCopyright = "DreamWorld Productions (c) 1996-1997.  Freely distributable.";
  18. BYTE *ProgShort     = "3-Dimensional object demonstration.";
  19.  
  20. struct GScreen *screen;
  21. struct JoyData *jport1;
  22.  
  23. void Demo(void);
  24.  
  25. struct DotPixel { double X,Y,Z; };
  26.  
  27. #define AMTCOLOURS 32
  28.  
  29. LONG palette[AMTCOLOURS+2] = {
  30.   PALETTE,32,
  31.   0x000000L,0x101010L,0x171717L,0x202020L,0x272727L,0x303030L,0x373737L,0x404040L,
  32.   0x474747L,0x505050L,0x575757L,0x606060L,0x676767L,0x707070L,0x777777L,0x808080L,
  33.   0x878787L,0x909090L,0x979797L,0xa0a0a0L,0xa7a7a7L,0xb0b0b0L,0xb7b7b7L,0xc0c0c0L,
  34.   0xc7c7c7L,0xd0d0d0L,0xd7d7d7L,0xe0e0e0L,0xe0e0e0L,0xf0f0f0L,0xf7f7f7L,0xffffffL
  35. };
  36.  
  37. /***********************************************************************************/
  38.  
  39. void main(void)
  40. {
  41.   if (screen = InitTags(NULL,
  42.      TAGS_SCREEN,    NULL,
  43.        GSA_BitmapTags, NULL,
  44.        BMA_AmtColours, AMTCOLOURS,
  45.        TAGEND, NULL,
  46.      GSA_Palette,    palette,
  47.      GSA_Attrib,     DBLBUFFER,
  48.      TAGEND)) {
  49.  
  50.      if (jport1 = Init(Get(ID_JOYDATA),NULL)) {
  51.         Display(screen);
  52.         Demo();
  53.  
  54.      Free(jport1);
  55.      }
  56.   Free(screen);
  57.   }
  58. }
  59.  
  60. /************************************************************************************
  61. ** Longtitude deterimines the position of the dot on the horizontal axis.
  62. ** Latitude determines the position of the dot on the vertical axis.
  63. */
  64.  
  65. #define AMTDOTS 1000       /* The amount of dots in the Object. */
  66.  
  67. #define MAXZ 1.96          /* MAXZ defined by 1.4*1.4 = 1.96 */
  68.  
  69. void Demo(void)
  70. {
  71.   struct DotPixel *object;
  72.   WORD   i;
  73.   WORD   offsetx = (screen->Width/2);
  74.   WORD   offsety = (screen->Height/2);
  75.   double temp;
  76.   double angle=0;
  77.   LONG  colour;
  78.   double Z2,X2,Y2;
  79.   LONG   scale=32;
  80.   UWORD  anglex=0,angley=0,anglez=0;
  81.   double u,v;
  82.   double *sine;       /* Pointer to our sine table */
  83.   double *cosine;     /* Pointer to our cosine table */
  84.  
  85.   object = AllocMemBlock(sizeof(struct DotPixel)*AMTDOTS,MEM_DATA);
  86.   sine   = AllocMemBlock(sizeof(double)*360,MEM_DATA);
  87.   cosine = AllocMemBlock(sizeof(double)*360,MEM_DATA);
  88.  
  89.   /* First calculate the X, Y and Z coordinates of our object. */
  90.  
  91.   for (i=0; i<AMTDOTS; i++) {
  92.     u = ((double)FastRandom(28000-1)+1)/10000-1.4;  /*  -1.4 < u < 1.4  */
  93.     v = ((double)FastRandom(28000-1)+1)/10000-1.4;  /*  -1.4 < v < 1.4  */
  94.     object[i].X  = u*(1+v*v)-u*u*u;
  95.     object[i].Y  = v*(1+u*u)-v*v*v;
  96.     object[i].Z  = u*u-v*v;
  97.   }
  98.  
  99.   /* Now generate our cosine and sinus tables */
  100.  
  101.   for (i=0; i<360; i++) {
  102.     cosine[i] = cos(angle);
  103.     sine[i]   = sin(angle);
  104.     angle    += 0.25;
  105.   }
  106.  
  107.   /* Go into our main loop */
  108.  
  109.   do
  110.   {
  111.     Query(jport1);
  112.     scale += jport1->YChange;
  113.     if (scale < 1)   scale = 1;
  114.     if (scale > 100) scale = 100;
  115.  
  116.     ClearBitmap(screen->Bitmap);
  117.  
  118.     for (i=0; i<AMTDOTS; i++) {
  119.  
  120.       X2 = object[i].X;
  121.       Y2 = object[i].Y;
  122.       Z2 = object[i].Z;
  123.  
  124.       /* Rotate the X axis */
  125.  
  126.       temp = Z2;
  127.       Z2 = Z2*cosine[anglex] - Y2*sine[anglex];
  128.       Y2 = Y2*cosine[anglex] + temp*sine[anglex];
  129.  
  130.       /* Rotate the Y axis */
  131.  
  132.       temp = Z2;
  133.       Z2 = Z2*cosine[angley] - X2*sine[angley];
  134.       X2 = X2*cosine[angley] + temp*sine[angley];
  135.  
  136.       /* Rotate the Z axis */
  137.  
  138. //      temp = X2;
  139. //      X2 = X2*cosine[anglez] - Y2*sine[anglez];
  140. //      Y2 = Y2*cosine[anglez] + temp*sine[anglez];
  141.  
  142.       /* Calculate colour based on Z position (-1.96 < Z < +1.96) */
  143.  
  144.       colour = (((Z2+MAXZ)/MAXZ)*screen->Bitmap->AmtColours)/2;
  145.  
  146.       /* Finally scale the (x,y) coordinates to enlarge or shrink the sphere */
  147.  
  148.       X2 *= scale;
  149.       Y2 *= scale;
  150.  
  151.       DrawPixel(screen->Bitmap,(WORD)X2+offsetx,(WORD)Y2+offsety,colour);
  152.     }
  153.  
  154.     anglex++; if (anglex >= 360) anglex = 0;
  155.     angley++; if (angley >= 360) angley = 0;
  156.     anglez++; if (anglez >= 360) anglez = 0;
  157.  
  158.     WaitAVBL();
  159.     SwapBuffers(screen);
  160.   } while(!(jport1->Buttons & JD_LMB));
  161.  
  162.   FreeMemBlock(object);
  163.   FreeMemBlock(sine);
  164.   FreeMemBlock(cosine);
  165. }
  166.  
  167.